home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZFixedAllocator.h < prev    next >
Text File  |  1997-09-02  |  3KB  |  115 lines

  1. /*
  2.  *  File:       ZFixedAllocator.h
  3.  *  Summary:    A class that can speedily allocate fixed size blocks.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <2>     7/13/97    JDJ        Inlined HasBlock.
  12.  *         <1>     1/29/96    JDJ        Created
  13.  */
  14.  
  15. #pragma once
  16.  
  17. #include <ZDebug.h>
  18. #include <ZTypes.h>
  19.  
  20.  
  21. //-----------------------------------
  22. //    Forward References
  23. //
  24. struct SBlock;
  25.  
  26.  
  27. //-----------------------------------
  28. //    Types
  29. //
  30. typedef void (*BlockValidateHook)(const void* block, long size, void* refCon);
  31.  
  32.  
  33. // ===================================================================================
  34. //    class TFixedAllocator
  35. // ===================================================================================
  36. class TFixedAllocator {
  37.  
  38. //-----------------------------------
  39. //    Initialization/Destruction
  40. //
  41. public:
  42.                         ~TFixedAllocator();
  43.  
  44.                         TFixedAllocator(ulong blockSize, ulong maxBlocks);
  45.                         
  46. private:
  47.                         TFixedAllocator(const TFixedAllocator& rhs);
  48.                         
  49.             TFixedAllocator& operator=(const TFixedAllocator& rhs);
  50.                 
  51. //-----------------------------------
  52. //    Allocations
  53. //
  54. public:
  55.             void*        Allocate();
  56.                         
  57.             void         Deallocate(void* ptr);
  58.  
  59. //-----------------------------------
  60. //    Info
  61. //
  62. public:
  63.             ulong         GetHeapSize() const                        {return mBlockSize*mMaxBlocks;}
  64.             
  65.             ulong         GetCurrentPercent() const                {return 100*mBlockCount/mMaxBlocks;}
  66.                         // Returns the number of blocks as a percentage of the max blocks.
  67.             
  68.             ulong         GetMaxPercent() const                    {return 100*mMaxBlockCount/mMaxBlocks;}
  69.                         // Returns the largest number of blocks as a percentage of the max blocks.
  70.             
  71.             ulong         GetBlockSize() const                    {return mBlockSize;}
  72.             
  73.             bool         HasBlock(const void* ptr) const;
  74.                         // Returns true if the block belongs to 'this'.
  75.  
  76. #if DEBUG
  77.     virtual void         ValidateHeap(BlockValidateHook hook = nil, void* refCon = nil) const;
  78. #endif
  79.  
  80. //-----------------------------------
  81. //    Internal API
  82. //
  83. protected:
  84.             SBlock*     GetNextBlock(SBlock* block) const;
  85.             
  86. //-----------------------------------
  87. //    Member Data
  88. //
  89. protected:    
  90.     ulong        mBlockSize;
  91.     ulong        mMaxBlocks;
  92.     
  93.     SBlock*        mBlocks;
  94.     ulong        mBlockCount;
  95.     ulong        mMaxBlockCount;
  96.  
  97.     SBlock*        mNextFreeBlock;
  98.     SBlock*        mEndBlock;
  99. };
  100.  
  101.  
  102. // ===================================================================================
  103. //    Inlines
  104. // ===================================================================================
  105. inline bool TFixedAllocator::HasBlock(const void* ptr) const        // this is often called *lots* of times
  106. {
  107.     ASSERT(ptr != nil);
  108.     
  109.     const SBlock* block = reinterpret_cast<const SBlock*>(ptr);
  110.     
  111.     bool has = block >= mBlocks && block < mEndBlock;
  112.     
  113.     return has;
  114. }
  115.